clear;
clc;
close all;
addpath('../02_functions');

%------------------------------------------------------------------------%
% This file creates table 9 in the paper 
%------------------------------------------------------------------------%

% choose sample period ---------------------------------------------------------------------------------- %

start_date              = datenum(2004,1,5); 
finish_date             = datenum(2020,12,29);

winsorizing             = 0;
alpha                   = 0.99;
replace_option          = 1;

file_name               = '../01_data/es_quotes_5minute_EST';
results                 = make_returns_288(file_name,start_date,finish_date,winsorizing,alpha,replace_option);
return_AD               = results.return_AD;
log_return_AD           = results.log_return_AD;
T                       = results.T;
caldt                   = results.caldt;

% Compute returns------------------------------------------------------------------------------- %

file_name               = 'es_quotes_5minute_EST.mat';
data_quotes             = load(strcat('../01_Data/',file_name));

beg                     = find_closest(data_quotes.caldt_daily,start_date);
fin                     = find_closest(data_quotes.caldt_daily,finish_date);
    
caldt                   = data_quotes.caldt_daily(beg:fin);
log_Bid_price_last      = log(data_quotes.BID_mat);
Bid_price_last          = data_quotes.BID_mat;
log_Ask_price_last      = log(data_quotes.ASK_mat);
Ask_price_last          = data_quotes.ASK_mat;
log_Mid_price_last      = log(data_quotes.MID_mat);
Mid_price_last          = data_quotes.MID_mat;



% Define time periods of interest:
ODIndex_start               = 96;         % 02:00 
ODIndex_end                 = 108;        % 03:00 
log_return_OD_TC            = log_Bid_price_last(beg:fin,ODIndex_end) - log_Ask_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 96;         % 02:00 
ODIndex_end                 = 108;        % 03:00 
return_OD_TC                = (Bid_price_last(beg:fin,ODIndex_end) - Ask_price_last(beg:fin,ODIndex_start))./Ask_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 96;         % 02:00 
ODIndex_end                 = 108;        % 03:00 
log_return_OD               = log_Mid_price_last(beg:fin,ODIndex_end) - log_Mid_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 96;         % 02:00 
ODIndex_end                 = 108;        % 03:00 
return_OD                   = (Mid_price_last(beg:fin,ODIndex_end) - Mid_price_last(beg:fin,ODIndex_start))./Mid_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 90;         % 01:30 
ODIndex_end                 = 114;        % 03:30 
log_return_OD_extended_TC   = log_Bid_price_last(beg:fin,ODIndex_end) - log_Ask_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 90;         % 01:30 
ODIndex_end                 = 114;        % 03:30 
return_OD_extended_TC       = (Bid_price_last(beg:fin,ODIndex_end) - Ask_price_last(beg:fin,ODIndex_start))./Ask_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 90;         % 01:30 
ODIndex_end                 = 114;        % 03:30 
log_return_OD_extended      = log_Mid_price_last(beg:fin,ODIndex_end) - log_Mid_price_last(beg:fin,ODIndex_start); 

ODIndex_start               = 90;         % 01:30 
ODIndex_end                 = 114;        % 03:30 
return_OD_extended          = (Mid_price_last(beg:fin,ODIndex_end) - Mid_price_last(beg:fin,ODIndex_start))./Mid_price_last(beg:fin,ODIndex_start); 

IDIndex_start               = 186;        % 09:30
IDIndex_end                 = 267;        % 16:15
log_return_OTC_TC           = log_Bid_price_last(beg:fin,IDIndex_end) - log_Ask_price_last(beg:fin,IDIndex_start); 

IDIndex_start               = 186;        % 09:30
IDIndex_end                 = 267;        % 16:15
return_OTC_TC               = (Bid_price_last(beg:fin,IDIndex_end) - Ask_price_last(beg:fin,IDIndex_start))./Ask_price_last(beg:fin,IDIndex_start); 

IDIndex_start               = 186;        % 09:30
IDIndex_end                 = 267;        % 16:15
log_return_OTC              = log_Mid_price_last(beg:fin,IDIndex_end) - log_Mid_price_last(beg:fin,IDIndex_start); 

IDIndex_start               = 186;        % 09:30
IDIndex_end                 = 267;        % 16:15
return_OTC                  = (Mid_price_last(beg:fin,IDIndex_end) - Mid_price_last(beg:fin,IDIndex_start))./Mid_price_last(beg:fin,IDIndex_start); 

ONIndex_start               = 267;      % 16:15
ONIndex_end                 = 186;      % 09:30
log_return_CTO_TC           = log_Bid_price_last(beg:fin,ONIndex_end) - log_Ask_price_last(beg-1:fin-1,ONIndex_start); 

ONIndex_start               = 267;      % 16:15
ONIndex_end                 = 186;      % 09:30
return_CTO_TC               = (Bid_price_last(beg:fin,ONIndex_end) - Ask_price_last(beg-1:fin-1,ONIndex_start))./Ask_price_last(beg-1:fin-1,ONIndex_start); 

ONIndex_start               = 267;      % 16:15
ONIndex_end                 = 186;      % 09:30
log_return_CTO = log_Mid_price_last(beg:fin,ONIndex_end) - log_Mid_price_last(beg-1:fin-1,ONIndex_start); 

ONIndex_start               = 267;      % 16:15
ONIndex_end                 = 186;      % 09:30
return_CTO                  = (Mid_price_last(beg:fin,ONIndex_end) - Mid_price_last(beg-1:fin-1,ONIndex_start))./Mid_price_last(beg-1:fin-1,ONIndex_start); 

ONIndex_start               = 267;      % 16:15
ONIndex_end                 = 267;          % 09:30
log_return_CTC              = log_Mid_price_last(beg:fin,ONIndex_end) - log_Mid_price_last(beg-1:fin-1,ONIndex_start); 

ONIndex_start               = 267;      % 16:15
ONIndex_end                 = 267;      % 09:30
return_CTC = (Mid_price_last(beg:fin,ONIndex_end) - Mid_price_last(beg-1:fin-1,ONIndex_start))./Mid_price_last(beg-1:fin-1,ONIndex_start); 

OpenIndex_start = 174;      % 08:30 
OpenIndex_end = 192;        % 10:00 
log_return_short_Open_TC = log_Bid_price_last(:,OpenIndex_start) - log_Ask_price_last(:,OpenIndex_end); 

OpenIndex_start             = 174;      % 08:30 
OpenIndex_end               = 192;      % 10:00 
return_short_Open_TC        = (Bid_price_last(:,OpenIndex_start) - Ask_price_last(:,OpenIndex_end))./Ask_price_last(:,OpenIndex_end); 

OpenIndex_start             = 174;      % 08:30 
OpenIndex_end               = 192;      % 10:00 
log_return_short_Open       = log_Mid_price_last(:,OpenIndex_start) - log_Mid_price_last(:,OpenIndex_end); 

OpenIndex_start             = 174;      % 08:30 
OpenIndex_end               = 192;      % 10:00 
return_short_Open           = (Mid_price_last(:,OpenIndex_start) - Mid_price_last(:,OpenIndex_end))./Mid_price_last(:,OpenIndex_end); 


% Risk-free returns (from 4 week Treasury Bills------------------------------------------------------------------------------- %

[num,txt,raw] =  xlsread('../01_Data/CRSP_DAILY_4WK_RF_V4.xlsx');

yield               = num(:,4);
dates_yield         = num(:,1)+693960;

beg_rf              = find_closest(dates_yield,start_date);
fin_rf              = find_closest(dates_yield,finish_date);

dates_yield_plot    = dates_yield(beg_rf:fin_rf);
ann_rf_return       = exp(365*yield) - 1  ;
daily_rf_return     = ann_rf_return/365;
daily_rf_return     = daily_rf_return(beg_rf:fin_rf);
cum_return_rf       = cumprod(1+daily_rf_return);
cum_return_rf       = cum_return_rf./cum_return_rf(1);

% ----------------------------------------------------------------% 
% LOAD TRADES
% ----------------------------------------------------------------% 

data_trades = load('../01_Data/es_trades_5minute_EST.mat');

No_trades                                           = data_trades.trades_per_min_mat;
No_contracts                                        = data_trades.trade_quantity_per_min_mat;
Signed_volume                                       = data_trades.signed_vol_per_min_mat;

beg_TRADES                                          = find_closest(data_trades.caldt_daily,start_date);
fin_TRADES                                          = find_closest(data_trades.caldt_daily,finish_date);

caldt_trades                                        = data_trades.caldt_daily(beg_TRADES:fin_TRADES);
No_trades                                           = No_trades(beg_TRADES:fin_TRADES,:);
Volume                                              = No_contracts(beg_TRADES:fin_TRADES,:);
Signed_volume                                       = Signed_volume(beg_TRADES:fin_TRADES,:);

% INTERSECT
[C,IA,IB]                                           = intersect(caldt_trades,caldt);
caldt                                               = caldt(IB);
log_return_AD                                       = log_return_AD(IB,:);
return_AD                                           = return_AD(IB,:);

log_return_OD_TC                                    = log_return_OD_TC(IB,:);
return_OD_TC                                        = return_OD_TC(IB,:);
log_return_OD                                       = log_return_OD(IB,:);
return_OD                                           = return_OD(IB,:);

log_return_OD_extended_TC                           = log_return_OD_extended_TC(IB,:);
return_OD_extended_TC                               = return_OD_extended_TC(IB,:);
log_return_OD_extended                              = log_return_OD_extended(IB,:);
return_OD_extended                                  = return_OD_extended(IB,:);

log_return_OTC_TC                                   = log_return_OTC_TC(IB,:);
return_OTC_TC                                       = return_OTC_TC(IB,:);
log_return_OTC                                      = log_return_OTC(IB,:);
return_OTC                                          = return_OTC(IB,:);

log_return_CTO_TC                                   = log_return_CTO_TC(IB,:);
return_CTO_TC                                       = return_CTO_TC(IB,:);
log_return_CTO                                      = log_return_CTO(IB,:);
return_CTO                                          = return_CTO(IB,:);

log_return_CTC                                      = log_return_CTC(IB,:);
return_CTC                                          = return_CTC(IB,:);

log_return_short_Open_TC                            = log_return_short_Open_TC(IB,:);
return_short_Open_TC                                = return_short_Open_TC(IB,:);
log_return_short_Open                               = log_return_short_Open(IB,:);
return_short_Open                                   = return_short_Open(IB,:);

caldt_trades                                        = caldt_trades(IA);
No_trades                                           = No_trades(IA,:);
Volume                                              = Volume(IA,:);
Signed_volume                                       = Signed_volume(IA,:);

% ----------------------------------------------------------------%
% % Load VIX indices
% ----------------------------------------------------------------%

file_name               = '../01_data/VIX_index_5min_EST.mat';
data_VIX                = load(file_name);

beg                     = find_closest(data_VIX.caldt_daily,start_date);
fin                     = find_closest(data_VIX.caldt_daily,finish_date);

caldt_VIX               = data_VIX.caldt_daily(beg:fin);
caldt_VIX_mat           = data_VIX.caldt_mat(beg:fin,:);
VIX                     = data_VIX.price_mat(beg:fin,:);

indx_beg                = find(hour(caldt_VIX_mat(1,:)) == 9 & minute(caldt_VIX_mat(1,:)) == 00);
indx_fin                = find(hour(caldt_VIX_mat(1,:)) == 16 & minute(caldt_VIX_mat(1,:)) == 15);
scaldt_ID               = caldt_VIX_mat(1,indx_beg:indx_fin);
caldt_ID.Format         = 'HH:mm';
VIX                     = VIX(:,indx_beg:indx_fin);

VIX_open                = VIX(:,1);
VIX_close               = VIX(:,end);

VIX_change1             = VIX_open(2:end) - VIX_close(1:end-1);
VIX_change2             = VIX_close(2:end) - VIX_open(2:end);
VIX_change3             = VIX_close(2:end) - VIX_close(1:end-1);
VIX                     = VIX_close;
caldt_VIX               = caldt_VIX(2:end,1);

% INTERSECT
[C,IA,IB]               = intersect(caldt_VIX,caldt);
caldt                   = caldt(IB);
log_return_AD           = log_return_AD(IB,:);
return_AD               = return_AD(IB,:);

log_return_OD_TC            = log_return_OD_TC(IB,:);
return_OD_TC                = return_OD_TC(IB,:);
log_return_OD               = log_return_OD(IB,:);
return_OD                   = return_OD(IB,:);

log_return_OD_extended_TC   = log_return_OD_extended_TC(IB,:);
return_OD_extended_TC       = return_OD_extended_TC(IB,:);
log_return_OD_extended      = log_return_OD_extended(IB,:);
return_OD_extended          = return_OD_extended(IB,:);

log_return_OTC_TC           = log_return_OTC_TC(IB,:);
return_OTC_TC               = return_OTC_TC(IB,:);
log_return_OTC              = log_return_OTC(IB,:);
return_OTC                  = return_OTC(IB,:);

log_return_CTO_TC           = log_return_CTO_TC(IB,:);
return_CTO_TC               = return_CTO_TC(IB,:);
log_return_CTO              = log_return_CTO(IB,:);
return_CTO                  = return_CTO(IB,:);

log_return_CTC              = log_return_CTC(IB,:);
return_CTC                  = return_CTC(IB,:);

log_return_short_Open_TC    = log_return_short_Open_TC(IB,:);
return_short_Open_TC        = return_short_Open_TC(IB,:);
log_return_short_Open       = log_return_short_Open(IB,:);
return_short_Open           = return_short_Open(IB,:);

caldt_trades                = caldt_trades(IB);
No_trades                   = No_trades(IB,:);
Volume                      = Volume(IB,:);
Signed_volume               = Signed_volume(IB,:);

caldt_VIX                   = caldt_VIX(IA,1);
VIX                         = VIX(IA,1);
VIX_change1                 = VIX_change1(IA,1);
VIX_change2                 = VIX_change2(IA,1);
VIX_change3                 = VIX_change3(IA,1);


% ----------------------------------------------------------------% 
% % COMPUTE RETURNS
% ----------------------------------------------------------------% 

% 15 min intervals
% 15 minute log returns
indx                        = [3 : 3 : length(Signed_volume(1,:))];
indx_loop                   = [0 , indx];
Signed_volume_15b15_AD      = nan(size(Signed_volume,1),24*4);
Volume_15b15_AD             = nan(size(Volume,1),24*4);

for H = 1 : round(length(Signed_volume(1,:))/12*4)
    Signed_volume_15b15_AD(:,H)         = sum(Signed_volume(:,indx_loop(H)+1 : indx_loop(H+1)),2);
    Volume_15b15_AD(:,H)                = sum(Volume(:,indx_loop(H)+1 : indx_loop(H+1)),2);
end

lead = 1;

sum_signedVol_start                 = 86;
sum_signedVol_end                   = 89;

log_return_OD                       = log_return_OD(1+lead:end);
log_return_OD_extended              = log_return_OD_extended(1+lead:end);
log_return_OTC                      = log_return_OTC(1+lead:end);
log_return_CTO                      = log_return_CTO(1+lead:end);
log_return_CTC                      = log_return_CTC(1+lead:end);
log_return_short_Open               = log_return_short_Open(1+lead:end);

return_OD                           = return_OD(1+lead:end);
return_OD_extended                  = return_OD_extended(1+lead:end);
return_OTC                          = return_OTC(1+lead:end);
return_CTO                          = return_CTO(1+lead:end);
return_CTC                          = return_CTC(1+lead:end);
return_short_Open                   = return_short_Open(1+lead:end);

log_return_OD_TC                    = log_return_OD_TC(1+lead:end);
log_return_OD_extended_TC           = log_return_OD_extended_TC(1+lead:end);
log_return_OTC_TC                   = log_return_OTC_TC(1+lead:end);
log_return_CTO_TC                   = log_return_CTO_TC(1+lead:end);
log_return_short_Open_TC            = log_return_short_Open_TC(1+lead:end);

return_OD_TC                        = return_OD_TC(1+lead:end);
return_OD_extended_TC               = return_OD_extended_TC(1+lead:end);
return_OTC_TC                       = return_OTC_TC(1+lead:end);
return_CTO_TC                       = return_CTO_TC(1+lead:end);
return_short_Open_TC                = return_short_Open_TC(1+lead:end);

log_return_OD_extended_sort_lead_TC = log_return_OD_extended_TC;
return_OD_extended_sort_lead_TC     = return_OD_extended_TC;
log_return_OD_extended_sort_lead    = log_return_OD_extended;
return_OD_extended_sort_lead        = return_OD_extended;

close_signed_volume_lead            = sum(Signed_volume_15b15_AD(1:end-lead,sum_signedVol_start:sum_signedVol_end),2);
close_rel_signed_volume_lead        = sum(Signed_volume_15b15_AD(1:end-lead,sum_signedVol_start:sum_signedVol_end),2)./sum(Volume_15b15_AD(1:end-lead,sum_signedVol_start:sum_signedVol_end),2);
caldt_true_lead                     = caldt(1+lead:end);

caldt_true                          = caldt;
close_signed_volume                 = sum(Signed_volume_15b15_AD(:,sum_signedVol_start:sum_signedVol_end),2);
close_rel_signed_volume             = sum(Signed_volume_15b15_AD(:,sum_signedVol_start:sum_signedVol_end),2)./sum(Volume_15b15_AD(:,sum_signedVol_start:sum_signedVol_end),2);

VIX_change_lead                     = VIX_change3(1:end-lead);


%% strategies
indx_strat1                 = find(close_rel_signed_volume_lead < 0);
indx_strat2                 = find(VIX_change_lead > 0);
indx_strat3                 = find(close_rel_signed_volume_lead < 0 & VIX_change_lead > 0);

reg                         = fitlm(close_rel_signed_volume_lead,VIX_change_lead,'linear','Intercept',false);
b_coeff                     = table2array(reg.Coefficients(1,1));
indx_strat4                 = find(VIX_change_lead > -b_coeff*close_rel_signed_volume_lead);

reg2                        = fitlm(close_rel_signed_volume_lead,VIX_change_lead,'linear');
a_coeff                     = table2array(reg2.Coefficients(1,1));
b_coeff2                    = table2array(reg2.Coefficients(2,1));
indx_strat5                 = find(VIX_change_lead > -b_coeff2*close_rel_signed_volume_lead+a_coeff);
cccc                        = -1/b_coeff;

indx_strat9                 = find(VIX_change_lead > 100*close_rel_signed_volume_lead);



% ----------------------------------------------------------------% 
% Sorts
% ----------------------------------------------------------------% 


% find the days where we go long:

indx_B1                                 = indx_strat1;
indx_B2                                 = indx_strat2;
indx_B9                                 = indx_strat9;

percentage1                             = 100*length(indx_B1)/length(VIX_change_lead)
percentage2                             = 100*length(indx_B2)/length(VIX_change_lead)
percentage3                             = 100*length(indx_B9)/length(VIX_change_lead)

cum_return_CTC                          = cumprod(1+return_CTC);
cum_return_CTO                          = cumprod(1+return_CTO);
cum_return_OTC                          = cumprod(1+return_OTC);

cum_return_OR_short                     = cumprod(1+return_short_Open);
cum_return_OD                           = cumprod(1+return_OD);
cum_return_OD_extended                  = cumprod(1+return_OD_extended);

return_OD_TC                            = exp(log_return_OD_TC)-1;
cum_return_OD_TC                        = cumprod(1+return_OD_TC);

return_OD_extended_TC                   = exp(log_return_OD_extended_TC)-1;
cum_return_OD_extended_TC               = cumprod(1+return_OD_extended_TC);

return_CTO_TC                           = exp(log_return_CTO_TC)-1;
cum_return_CTO_TC                       = cumprod(1+return_CTO_TC);

return_OTC_TC                           = exp(log_return_OTC_TC)-1;
cum_return_OTC_TC                       = cumprod(1+return_OTC_TC);

return_short_Open_TC                    = exp(log_return_short_Open_TC)-1;
cum_return_short_Open_TC                = cumprod(1+return_short_Open_TC);

return_OD_sort_lead_RSV                 = return_OD_extended_sort_lead;
return_OD_long_RSV                      = zeros(size(return_OD_extended_sort_lead));
return_OD_long_RSV(indx_B1)             = return_OD_sort_lead_RSV(indx_B1);
cum_return_OD_long_RSV                  = cumprod(1+return_OD_long_RSV);
log_return_OD_long_RSV                  = zeros(size(log_return_OD_extended_sort_lead));
log_return_OD_long_RSV(indx_B1)         = log_return_OD_extended_sort_lead(indx_B1);

return_OD_sort_lead_TC_RSV              = return_OD_extended_sort_lead_TC;
return_OD_long_TC_RSV                   = zeros(size(return_OD_extended_sort_lead_TC));
return_OD_long_TC_RSV(indx_B1)          = return_OD_sort_lead_TC_RSV(indx_B1);
cum_return_OD_long_TC_RSV               = cumprod(1+return_OD_long_TC_RSV);
log_return_OD_long_TC_RSV               = zeros(size(log_return_OD_extended_sort_lead_TC));
log_return_OD_long_TC_RSV(indx_B1)      = log_return_OD_extended_sort_lead_TC(indx_B1);

return_OD_sort_lead_VIX                 = return_OD_extended_sort_lead;
return_OD_long_VIX                      = zeros(size(return_OD_extended_sort_lead));
return_OD_long_VIX(indx_B2)             = return_OD_sort_lead_VIX(indx_B2);
cum_return_OD_long_VIX                  = cumprod(1+return_OD_long_VIX);
log_return_OD_long_VIX                  = zeros(size(log_return_OD_extended_sort_lead));
log_return_OD_long_VIX(indx_B2)         = log_return_OD_extended_sort_lead(indx_B2);

return_OD_sort_lead_TC_VIX              = return_OD_extended_sort_lead_TC;
return_OD_long_TC_VIX                   = zeros(size(return_OD_extended_sort_lead_TC));
return_OD_long_TC_VIX(indx_B2)          = return_OD_sort_lead_TC_VIX(indx_B2);
cum_return_OD_long_TC_VIX               = cumprod(1+return_OD_long_TC_VIX);
log_return_OD_long_TC_VIX               = zeros(size(log_return_OD_extended_sort_lead_TC));
log_return_OD_long_TC_VIX(indx_B2)      = log_return_OD_extended_sort_lead_TC(indx_B2);

return_OD_sort_lead_RSV_VIX             = return_OD_extended_sort_lead;
return_OD_long_RSV_VIX                  = zeros(size(return_OD_extended_sort_lead));
return_OD_long_RSV_VIX(indx_B9)         = return_OD_sort_lead_RSV_VIX(indx_B9);
cum_return_OD_long_RSV_VIX              = cumprod(1+return_OD_long_RSV_VIX);
log_return_OD_long_RSV_VIX              = zeros(size(log_return_OD_extended_sort_lead));
log_return_OD_long_RSV_VIX(indx_B9)     = log_return_OD_extended_sort_lead(indx_B9);

return_OD_sort_lead_TC_RSV_VIX          = return_OD_extended_sort_lead_TC;
return_OD_long_TC_RSV_VIX               = zeros(size(return_OD_extended_sort_lead_TC));
return_OD_long_TC_RSV_VIX(indx_B9)      = return_OD_sort_lead_TC_RSV_VIX(indx_B9);
cum_return_OD_long_TC_RSV_VIX           = cumprod(1+return_OD_long_TC_RSV_VIX);
log_return_OD_long_TC_RSV_VIX           = zeros(size(log_return_OD_extended_sort_lead_TC));
log_return_OD_long_TC_RSV_VIX(indx_B9)  = log_return_OD_extended_sort_lead_TC(indx_B9);


%%
[caldt_intersect, IA, IB] = intersect(caldt_true_lead,caldt_true_lead);

%%VARIABLES FOR THE TABLE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Without trading costs
log_return_CTC_table                    = log_return_CTC(IA);
return_CTC_table                        = return_CTC(IA);
log_return_CTO_table                    = log_return_CTO(IA);
return_CTO_table                        = return_CTO(IA);
log_return_OTC_table                    = log_return_OTC(IA);
return_OTC_table                        = return_OTC(IA);
log_return_OR_short_table               = log_return_short_Open(IA);
return_OR_short_table                   = return_short_Open(IA);
log_return_OD_table                     = log_return_OD(IA);
return_OD_table                         = return_OD(IA);
log_return_OD_extended_table            = log_return_OD_extended(IA);
return_OD_extended_table                = return_OD_extended(IA);
log_return_OD_sort_lead_table_RSV       = log_return_OD_long_RSV(IA);
return_OD_sort_lead_table_RSV           = return_OD_long_RSV(IA);
log_return_OD_sort_lead_table_VIX       = log_return_OD_long_VIX(IA);
return_OD_sort_lead_table_VIX           = return_OD_long_VIX(IA);
log_return_OD_sort_lead_table_RSV_VIX   = log_return_OD_long_RSV_VIX(IA);
return_OD_sort_lead_table_RSV_VIX       = return_OD_long_RSV_VIX(IA);

% With trading costs
log_return_CTO_TC_table                     = log_return_CTO_TC(IA);
return_CTO_TC_table                         = return_CTO_TC(IA);
log_return_OTC_TC_table                     = log_return_OTC_TC(IA);
return_OTC_TC_table                         = return_OTC_TC(IA);
log_return_short_Open_TC_table              = log_return_short_Open_TC(IA);
return_short_Open_TC_table                  = return_short_Open_TC(IA);
log_return_OD_TC_table                      = log_return_OD_TC(IA);
return_OD_TC_table                          = return_OD_TC(IA);
log_return_OD_extended_TC_table             = log_return_OD_extended_TC(IA);
return_OD_extended_TC_table                 = return_OD_extended_TC(IA);
log_return_OD_sort_lead_TC_table_RSV        = log_return_OD_long_TC_RSV(IA);
return_OD_sort_lead_TC_table_RSV            = return_OD_long_TC_RSV(IA);
log_return_OD_sort_lead_TC_table_VIX        = log_return_OD_long_TC_VIX(IA);
return_OD_sort_lead_TC_table_VIX            = return_OD_long_TC_VIX(IA);
log_return_OD_sort_lead_TC_table_RSV_VIX    = log_return_OD_long_TC_RSV_VIX(IA);
return_OD_sort_lead_TC_table_RSV_VIX        = return_OD_long_TC_RSV_VIX(IA);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[caldt_intersect, IA, IB]       = intersect(caldt_true_lead,dates_yield_plot);

daily_rf_return_SR              = nan(size(caldt_true_lead));
for  i=1:length(IA)
    daily_rf_return_SR(IA(i))   = daily_rf_return(i);
end
daily_rf_return_SR              = fillmissing(daily_rf_return_SR,'previous');


%% table
return_matrix = [log_return_CTC_table log_return_CTO_table log_return_OTC_table log_return_OD_table log_return_OD_extended_table log_return_OD_sort_lead_table_RSV   ...
                 log_return_CTO_TC_table log_return_OTC_TC_table  log_return_OD_TC_table log_return_OD_extended_TC_table log_return_OD_sort_lead_TC_table_RSV  ];

return_matrix2 = [return_CTC_table return_CTO_table return_OTC_table return_OD_table return_OD_extended_table return_OD_sort_lead_table_RSV   ...
                 return_CTO_TC_table return_OTC_TC_table  return_OD_TC_table return_OD_extended_TC_table return_OD_sort_lead_TC_table_RSV  ];


table_values = nan(6,length(return_matrix(1,:)));

for i = 1:length(return_matrix(1,:))
    
    temp = return_matrix(:,i); % log returns
    temp2 = return_matrix2(:,i); % returns

    table_values(1,i) = 100*252*mean(temp2);
    table_values(2,i) = 100*sqrt(252)*std(temp2);
    table_values(3,i) = 252*nanmean((temp2 - daily_rf_return_SR),1)./sqrt(252*nanvar((temp2 - daily_rf_return_SR),1)); % SHARPE RATIO
    cov_ID            = cov(return_CTC_table,temp2); 
    table_values(4,i) = cov_ID(1,2)/var(return_CTC_table); %BETA
    table_values(5,i) = skewness(temp2);
    table_values(6,i) = kurtosis(temp2);
end 

rets_string         = {'$CTC$', '$CTO$', '$OTC$', '$OD$','$OD+$','$RSV^{close}_{t-1}<0$'};
stats_string        = {' ' , 'Mean','Sdev', 'Sharpe ratio', 'beta', 'Skew', 'Kurt'};
test                = table_values;
table_values        = round(table_values,2);
info.cnames         = strvcat(rets_string);
info.rnames         = strvcat(stats_string);
info.fmt            = '%6.2f';
info.swidth         = 220;
info.hspc           = 5;
mprint1(table_values(:,1:6), info);


rets_string         = {'$CTC$', '$CTO$', '$OTC$', '$OD$','$OD+$','$RSV^{close}_{t-1}<0$'};
stats_string        = {' ' , 'Mean','Sdev', 'Sharpe ratio', 'beta', 'Skew', 'Kurt'};
temp_table          = nan(size(table_values(:,7:end),1),size(table_values(:,7:end),2)+1);
temp_table(:,2:end) = table_values(:,7:end);
temp_table(:,1)     =  table_values(:,1);
temp_table          = round(temp_table,2);
info.cnames         = strvcat(rets_string);
info.rnames         = strvcat(stats_string);
info.fmt            = '%6.2f';
info.swidth         = 220;
info.hspc           = 5;
mprint1(temp_table, info);



